package com.lagou.guava.config;

import com.google.common.cache.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author shenxiang
 * @date 2020/9/23 19:41
 * @description
 */
@Configuration
public class GuavaConfig {

    @Bean
    public LoadingCache<String,Object> guavaConfiguration(){
        return CacheBuilder.newBuilder()
                .maximumSize(3).concurrencyLevel(Runtime.getRuntime().availableProcessors()).refreshAfterWrite(3600, TimeUnit.SECONDS)
                .recordStats().removalListener(new RemovalListener<Object, Object>() {
                    public void onRemoval(RemovalNotification<Object, Object> notification){
                        System.out.println(notification.getKey()+":"+notification.getCause());
                    }
                })
                .build(
                        new CacheLoader<String, Object>() {
                            @Override
                            public String load(String s) {
                                return s;
                            }
                        }
                );
    }
}
